﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Arizu.Repository;
using System.Data;
using System.Text;

namespace AiRiZu.User.handler
{
    public class Calendar : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.Params["roomId"] == null || string.IsNullOrEmpty(context.Request.Params["roomId"].ToString()))
            {
                context.Response.Write("error");
            }
            else
            {
                try
                {
                    

                    long roomId = long.Parse(context.Request.Params["roomId"].ToString());
                    string viewStart = context.Request.Params["viewStartDate"].ToString();
                    string viewEnd = context.Request.Params["viewEndDate"].ToString();

                    DateTime viewStartDate = DateTime.ParseExact(viewStart.Replace("/", ""), "MMddyyyy", null);
                    DateTime viewEndDate = DateTime.ParseExact(viewEnd.Replace("/", ""), "MMddyyyy", null);

                    viewStartDate = viewStartDate.AddDays(-1);

                    AirizuPrice airizuPrice = AirizuPrice.CreateInstance();
                    DataTable dtPrice = airizuPrice.GetPrice(roomId);

                    StringBuilder sBuilder = new StringBuilder();
                    sBuilder.Append("[");

                    if (dtPrice.Rows.Count > 0)
                    {
                        AirizuIndent airizuIndent = AirizuIndent.CreateInstance();

                        DataTable dtIndent = airizuIndent.GetOrderOnAfterByRoomId(roomId, viewStartDate.AddDays(-1).Ticks);
                        List<string> indentList = new List<string>();
                        if (dtIndent.Rows.Count > 0)
                        {
                            for (int i = 0; i < dtIndent.Rows.Count; i++)
                            {
                                DateTime dTime = new DateTime(long.Parse(dtIndent.Rows[i]["StartTime"].ToString()));
                                string dTimeStr = dTime.ToString("yyyy-MM-dd");
                                indentList.Add(dTimeStr);
                            }
                        }

                        while (viewStartDate.Ticks <= viewEndDate.Ticks)
                        {
                            if (viewStartDate.Ticks > System.DateTime.Now.AddDays(-1).Ticks)
                            {
                                dtPrice.Rows[0]["DayPrice"].ToString();
                                sBuilder.Append("{");
                                sBuilder.Append("\"title\":").Append("\"").Append(dtPrice.Rows[0]["DayPrice"]).Append("\", ");
                                DateTime startTime = new DateTime(viewStartDate.Ticks);
                                sBuilder.Append("\"start\":").Append("\"").Append(startTime.ToString("yyyy-MM-dd")).Append("\", ");

                                if (indentList.Contains(startTime.ToString("yyyy-MM-dd")))
                                {
                                    sBuilder.Append("\"className\":").Append("\"").Append("unavailable").Append("\"");
                                }
                                else
                                {
                                    sBuilder.Append("\"className\":").Append("\"").Append("available").Append("\"");
                                }
                                
                                sBuilder.Append("},");
                            }
                            
                            viewStartDate = viewStartDate.AddDays(1);
                        }
                    }
                    AirizuCalendar airizuCalendar = AirizuCalendar.CreateInstance();

                    DataTable dtCalendars = airizuCalendar.GetCalendars(roomId, viewStartDate.Ticks, viewEndDate.Ticks);

                    if (dtCalendars.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtCalendars.Rows.Count; i++)
                        {
                            sBuilder.Append("{");
                            sBuilder.Append("\"title\":").Append("\"").Append(dtCalendars.Rows[i]["Price"]).Append("\", ");
                            DateTime startTime = new DateTime(long.Parse(dtCalendars.Rows[i]["StartTime"].ToString()));
                            
                            sBuilder.Append("\"start\":").Append("\"").Append(startTime.ToString("yyyy-MM-dd")).Append("\", ");
                            sBuilder.Append("\"className\":").Append("\"").Append("available").Append("\"");
                            sBuilder.Append("},");
                        }
                    }

                    sBuilder.Remove(sBuilder.Length - 1, 1);
                    sBuilder.Append("]");

                    context.Response.Write(sBuilder.ToString());
                }
                catch (Exception)
                {

                    context.Response.Write("error");
                }
            } 
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
